package com.sunshine.geek.bytecode.tools;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

public class Bytes {

	
	public static short u1(byte b){
		short result = 0;
		result|= (b & 0x00ff);
		return result;
	}
	

	public static short u2(byte [] b){
		ByteBuffer bb  =ByteBuffer.allocate(2);
		bb.put(b);
		bb.flip();
		return bb.getShort();
	}
	

	public static int u4(byte []b){
		ByteBuffer bb  =ByteBuffer.allocate(4);
		bb.put(b);
		bb.flip();
		return bb.getInt();
	}

	public static long u8(byte []b){
		ByteBuffer bb  =ByteBuffer.allocate(8);
		bb.put(b);
		bb.flip();
		return bb.getLong();
	}
	
	
	public static int byte2Int(byte[] bytes) {
		ByteBuffer bf = ByteBuffer.allocate(4);
		bf.put(bytes);
		bf.flip();
		return bf.getInt();
	}

	public static long byte2Long(byte[] bytes) {
		ByteBuffer bf = ByteBuffer.allocate(8);
		bf.put(bytes);
		bf.flip();
		return bf.getLong();
	}

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	public final static short getShort(byte[] buf, boolean asc) {
		int len = buf.length;
		short r = 0;
		if (asc)
			for (int i = len - 1; i >= 0; i--) {
				r <<= 8;
				r |= (buf[i] & 0x00ff);
			}
		else
			for (int i = 0; i < len; i++) {
				r <<= 8;
				r |= (buf[i] & 0x00ff);
			}
		return r;
	}

	public static String byteToBinaryString(byte b) {
		String binaryString = Integer.toBinaryString(b & 0xff);
		int number = binaryString.length();
		if (number == 8)
			return binaryString;
		return Integer.toBinaryString(((0xff >> number))).replaceAll("1", "0")
				+ binaryString;
	}
	
	public static String byte2String(byte [] b){
		try {
			return new String(b,"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static byte[] getBytes(short data)  
    {  
        byte[] bytes = new byte[2];  
        bytes[0] = (byte) (data & 0xff);  
        bytes[1] = (byte) ((data & 0xff00) >> 8);  
        return bytes;  
    }  
  
    public static byte[] getBytes(char data)  
    {  
        byte[] bytes = new byte[2];  
        bytes[0] = (byte) (data);  
        bytes[1] = (byte) (data >> 8);  
        return bytes;  
    }  
  
    public static byte[] getBytes(int data)  
    {  
        byte[] bytes = new byte[4];  
        bytes[0] = (byte) (data & 0xff);  
        bytes[1] = (byte) ((data & 0xff00) >> 8);  
        bytes[2] = (byte) ((data & 0xff0000) >> 16);  
        bytes[3] = (byte) ((data & 0xff000000) >> 24);  
        return bytes;  
    }  
  
    public static byte[] getBytes(long data)  
    {  
        byte[] bytes = new byte[8];  
        bytes[0] = (byte) (data & 0xff);  
        bytes[1] = (byte) ((data >> 8) & 0xff);  
        bytes[2] = (byte) ((data >> 16) & 0xff);  
        bytes[3] = (byte) ((data >> 24) & 0xff);  
        bytes[4] = (byte) ((data >> 32) & 0xff);  
        bytes[5] = (byte) ((data >> 40) & 0xff);  
        bytes[6] = (byte) ((data >> 48) & 0xff);  
        bytes[7] = (byte) ((data >> 56) & 0xff);  
        return bytes;  
    }  
  
    public static byte[] getBytes(float data)  
    {  
        int intBits = Float.floatToIntBits(data);  
        return getBytes(intBits);  
    }  
  
    public static byte[] getBytes(double data)  
    {  
        long intBits = Double.doubleToLongBits(data);  
        return getBytes(intBits);  
    }  
  

  

    public static short getShort(byte[] bytes)  
    {  
        return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
    }  
  
    public static char getChar(byte[] bytes)  
    {  
        return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
    }  
  
    public static int getInt(byte[] bytes)  
    {  
        return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));  
    }  
     
    public static long getLong(byte[] bytes)  
    {  
        return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))  
         | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));  
    }  
  
    public static float getFloat(byte[] bytes)  
    {  
        return Float.intBitsToFloat(getInt(bytes));  
    }  
  
    public static double getDouble(byte[] bytes)  
    {  
        long l = getLong(bytes);  
        System.out.println(l);  
        return Double.longBitsToDouble(l);  
    }  
  

  
      
}
